package com.example.itas;

import java.util.ArrayList;

import android.R.integer;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;

public class PcroomBackgroundView extends ImageView{

	private Bitmap pcAvailImage, pcNotAvailImage, pcSelectImage;
	private int seatImageW, seatImageH;
	private ArrayList<Integer> setSeatW;
	private ArrayList<Integer> setSeatH;
	private ArrayList<Integer> seatNum;
	private ArrayList<Integer> seatMode;
	
	public static final int PC_AVAILABLE = 1;
	public static final int PC_SELECTED = 2;
	public static final int PC_NOT_AVAILABLE = 0;
	
	public PcroomBackgroundView(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
		initialize();
		// TODO Auto-generated constructor stub
	}

	public PcroomBackgroundView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		initialize();
	}

	public PcroomBackgroundView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		initialize();
	}
	
	public ArrayList<Integer> getSetSeatW()	{ return setSeatW;}
	public ArrayList<Integer> getSetSeatH()	{ return setSeatH;}
	public ArrayList<Integer> getSeatNum()	{ return seatNum;}
	public ArrayList<Integer> getSeatMode() { return seatMode;}
	public int getSeatImageW() { return seatImageW;} 
	public int getSeatImageH() { return seatImageH;}
	
	public void initialize()
	{
		pcAvailImage = BitmapFactory.decodeResource(this.getResources(), R.drawable.pc_available);
		pcNotAvailImage = BitmapFactory.decodeResource(this.getResources(), R.drawable.pc_not_available);
		pcSelectImage = BitmapFactory.decodeResource(this.getResources(), R.drawable.pc_selected);
		seatImageW = pcAvailImage.getWidth();
		seatImageH = pcAvailImage.getHeight();
		setSeatW = new ArrayList<Integer>();
		setSeatH = new ArrayList<Integer>();
		seatNum = new ArrayList<Integer>();
		seatMode = new ArrayList<Integer>();
	}
	
	public void setSeat(int left, int top, int num, int mode)
	{
		setSeatW.add(left);
		setSeatH.add(top);
		seatNum.add(num);
		seatMode.add(mode);
		invalidate();
	}
	
	public void setSeat(int pcNum, int mode)
	{
		for(int i = 0; i<seatMode.size();++i)
		{
			if(seatNum.get(i) == pcNum)
			{
				seatMode.set(i, mode);
				invalidate();
				break;
			}
		}
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		//Log.i("DEBUG",bm.getWidth()+" "+bm.getHeight()); 
		if(!setSeatW.isEmpty() && !setSeatH.isEmpty())
		{
			for(int i = 0; i < setSeatH.size(); ++i)
			{	
				switch(seatMode.get(i))
				{
				case PC_AVAILABLE:
					canvas.drawBitmap(pcAvailImage,seatImageW*setSeatW.get(i),seatImageH*setSeatH.get(i),null);
					break;
				case PC_NOT_AVAILABLE:
					canvas.drawBitmap(pcNotAvailImage,seatImageW*setSeatW.get(i),seatImageH*setSeatH.get(i),null);
					break;
				case PC_SELECTED:
					canvas.drawBitmap(pcSelectImage,seatImageW*setSeatW.get(i),seatImageH*setSeatH.get(i),null);
					break;
				}
			}
		}
	}
}
